﻿using JaxaraRnD.Helpers.DataAccess;
using JaxaraRnD.Models.Objects;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;

namespace JaxaraRnD.Controllers
{
    public class OrderController : Controller
    {
        private readonly IRepository<Order> _orderRepository;
        private readonly IRepository<Product> _productRepository;
        private readonly IRepository<Staff> _staffRepository;
        public OrderController()
        {
            _orderRepository = new Repository<Order>();
            _productRepository = new Repository<Product>();
            _staffRepository = new Repository<Staff>();
        }
        //
        // GET: /Order/

        public ActionResult Index()
        {
            return View(_orderRepository.GetAll());
        }

        //
        // GET: /Order/Details/5

        public ActionResult Details(int id = 0)
        {
            Order order = _orderRepository.Get(id);
            if (order == null)
            {
                return HttpNotFound();
            }
            return View(order);
        }

        //
        // GET: /Order/Create

        public ActionResult Create()
        {
            Order order = new Order();
            ViewBag.ProductSelectList = GetProductSelectList(order);                     
            ViewBag.StaffId = PopulateStaffsDropDownList();
            return View(order);
        }

        public List<SelectListItem> GetProductSelectList(Order order)
        {
            var allProducts = _productRepository.GetAll();
            var products = allProducts.Select(p => new SelectListItem()
            {
                Text = p.Name.ToString(),
                Value = p.Id.ToString(),
                Selected = order.Products.FirstOrDefault(o => p.Id == o.Id) != null
            }).OrderBy(p => p.Text).ToList();

            return products;
        }
        private SelectList PopulateStaffsDropDownList(object selectedStaff = null)
        {
            return new SelectList(_staffRepository.GetAll(), "Id", "Name", selectedStaff);
        }
        //
        // POST: /Order/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            Order order = new Order();

            if (TryUpdateModel(order) && ModelState.IsValid)
            {
                order.Products = new List<Product>();

                var sProducts = collection["Products.List"];
                if (sProducts != null)
                {
                    int[] selectedProducts = sProducts.Split(new[] { ',' }).Select(n => Convert.ToInt32(n)).ToArray();

                    order.Products = _orderRepository.GetProductsByProductIds(selectedProducts);
                }
                var StaffId = collection["Staffs"];
                if (StaffId != null)
                {
                    order.Staff = _orderRepository.GetStaffByStaffId(Convert.ToInt32(StaffId));
                }
                _orderRepository.SaveOrUpdate(order);

                return RedirectToAction("Index");
            }           
            return View();
        }

        //
        // GET: /Order/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Order order = _orderRepository.Get(id);
            ViewBag.ProductSelectList = GetProductSelectList(order);
            if (order.Staff != null)
                ViewBag.StaffId = PopulateStaffsDropDownList(order.Staff.Id);
            else
                ViewBag.StaffId = PopulateStaffsDropDownList();
            return View(order);
        }

        //
        // POST: /Order/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            Order order = _orderRepository.Get(id);

            if (TryUpdateModel(order) && ModelState.IsValid)
            {
                order.Products = new List<Product>();
                var sProducts = collection["Products.List"];
                if (sProducts != null)
                {
                    var selectedProducts = sProducts.Split(new[] { ',' }).Select(n => Convert.ToInt32(n)).ToArray();

                    order.Products = _orderRepository.GetProductsByProductIds(selectedProducts);
                }
                var StaffId = collection["Staffs"];
                if (StaffId != null)
                {
                    order.Staff = _orderRepository.GetStaffByStaffId(Convert.ToInt32(StaffId));
                }
                _orderRepository.SaveOrUpdate(order);
            }

            return RedirectToAction("Index");
        }

        //
        // GET: /Order/Delete/5

        public ActionResult Delete(int id = 0)
        {
            _orderRepository.Delete(id);
            return RedirectToAction("Index");
        }     
    }
}